<?php

require_once 'common/conn.php';
require_once 'common/common.php';
require_once 'share.action.php';

$result = array();
$db = new DBC();
$usercode = getParameter("usercode");
$nickname = getParameter("nickname");
$sex = getParameter("sex");
$email = getParameter("email");
$password = getParameter("password");
$password2 = getParameter("password2");
$os = getParameter("os");

$is_exist = false;

if (getParameter('from') == "web") {
    if ($usercode != "") {
        $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "' OR username = '" . $usercode . "'";
        $data = $db->fetchData($sql);
        if (checkArrayNotEmpty($data)) {
            $is_exist = true;
        }
    }
    $email_exist = false;
    $nickname_exist = false;
    $email_wrong = false;
    $passwordNotSame = false;
    $nickname_Chinese = false;
    if ($is_exist) {

    } else {
        if ($email != "" && $nickname != "") {
            if (preg_match("/[\x7f-\xff]/", $nickname)) {
                $nickname_Chinese = true;
            } else if (funcemail($email)) {
                $sql = "SELECT nickname,email FROM t_device WHERE nickname = '" . $nickname . "' OR email = '" . $email . "'";
                $data = $db->fetchData($sql);
                if (checkArrayNotEmpty($data)) {
                    if (!isNull($data[0]['nickname']) && $data[0]['nickname'] == $nickname) {
                        $nickname_exist = true;
                    } else if (isNull($data[0]['email']) && $data[0]['email'] == $email) {
                        $email_exist = true;
                    }
                }
            } else {
                $email_wrong = true;
            }
        }

        if (!isNull($password2)) {
            if ($password != $password2) {
                $passwordNotSame = true;
            }
        }
        if ($nickname_exist) {
            $result['errorNo'] = "1";
            $result['errorMsg'] = "昵称 " . $nickname . " 已经被使用。";
        } else if ($email_exist) {
            $result['errorNo'] = "1";
            $result['errorMsg'] = "邮箱 " . $email . " 已经被使用。";
        } else if ($email_wrong) {
            $result['errorNo'] = "4";
            $result['errorMsg'] = "邮箱 " . $email . " 格式不正确。";
        } else if ($passwordNotSame) {
            $result['errorNo'] = "3";
            $result['errorMsg'] = "两次输入密码不同";
        } else if ($nickname_Chinese) {
            $result['errorNo'] = "3";
            $result['errorMsg'] = "昵称请勿使用中文";
        } else {
            $sql = "INSERT INTO t_device(devicecode,sex,password,email,nickname,createTime) VALUES('" . $usercode . "'," . $sex . ",'" . $password . "','" . $email . "','" . $nickname . "',now())";
            $id = $db->insertId($sql);
            if ($id > 0) {
                $share = array();
                $share['usercode'] = $usercode;
                $share['type'] = 1;
                $share['message'] = "我完成了注册任务。";
                updateShare($share);

                $result['errorNo'] = "0";
                $result['errorMsg'] = "注册成功";
            } else {
                $result['errorNo'] = "1";
                $result['errorMsg'] = "注册失败";
            }
        }
    }
    echo "<script language='javascript'>alert('" . $result['errorMsg'] . "');</script>";
} else {
    if ($usercode != "") {
        $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "' OR username = '" . $usercode . "'";
        $data = $db->fetchData($sql);
        if (checkArrayNotEmpty($data)) {
            $is_exist = true;
        }
    }
    if ($email != "" && $nickname != "") {
        $email_exist = false;
        $nickname_exist = false;
        $sql = "SELECT nickname,email FROM t_device WHERE nickname = '" . $nickname . "' OR email = '" . $email . "'";
        $data = $db->fetchData($sql);
        if (checkArrayNotEmpty($data)) {
            if (!isNull($data[0]['nickname'])) {
                $nickname_exist = true;
            } else if (isNull($data[0]['email'])) {
                $email_exist = true;
            }
        }

        if ($nickname_exist) {
            $result['errorNo'] = "1";
            $result['errorMsg'] = "昵称 " . $nickname . " 已经被使用。";
        } else if ($email_exist) {
            $result['errorNo'] = "1";
            $result['errorMsg'] = "邮箱 " . $email . " 已经被使用。";
        } else {
            $passwordNotSame = false;
            if (!isNull($password2)) {
                if ($password != $password2) {
                    $passwordNotSame = true;
                }
            }
            if (!$passwordNotSame) {
                if ($is_exist) {
                    $sql = "UPDATE t_device SET sex = " . $sex . ",password = '" . $password . "',email = '" . $email . "',nickname = '" . $nickname . "',createTime = now(), os = '" . $os . "' WHERE devicecode = '" . $usercode . "'";
                    if ($db->execute($sql)) {
                        $result['errorNo'] = "0";
                        $result['errorMsg'] = "注册成功";
                    } else {
                        $result['errorNo'] = "1";
                        $result['errorMsg'] = "注册失败";
                    }
                } else {
                    $sql = "INSERT INTO t_device(devicecode,sex,password,email,nickname,createTime,os) VALUES('" . $usercode . "'," . $sex . ",'" . $password . "','" . $email . "','" . $nickname . "',now(),'" . $os . "')";
                    $id = $db->insertId($sql);
                    if ($id > 0) {
                        $share = array();
                        $share['usercode'] = $usercode;
                        $share['type'] = 1;
                        $share['message'] = "我完成了注册任务。";
                        updateShare($share);

                        $result['errorNo'] = "0";
                    } else {
                        $result['errorNo'] = "1";
                        $result['errorMsg'] = "注册失败";
                    }
                }
            } else {
                $result['errorNo'] = "3";
                $result['errorMsg'] = "两次输入密码不同";
            }
        }
    } else {
        if ($is_exist) {
            $result['errorNo'] = 2;
            $result['errorMsg'] = "该用户已经存在";
        } else {
            if (isset($usercode)) {
                $sql = "INSERT INTO t_device(devicecode,createTime, os) VALUES('" . $usercode . "',now(),'" . $os . "')";
                $id = $db->insertId($sql);
                if ($id > 0) {
                    $result['errorNo'] = 0;
                } else {
                    $result['errorNo'] = 1;
                    $result['errorMsg'] = "注册失败";
                }
            }
        }
    }
    echo json_encode($result);
}

function funcemail($str) {//邮箱正则表达式
    return (preg_match('/^[_.0-9a-z-a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/', $str)) ? true : false;
}

?>
